package com.ruoyi.common.utils.ping;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

public class LinkChecker {

    public static void main(String[] args) {
        String url = "https://blog.csdn.net/drose29/article/dails/12597"; // 要检测的链接

        try {
            if (isLinkAccessible(url)) {
                System.out.println("链接可访问！");
            } else {
                System.out.println("链接不可访问！");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static boolean isLinkAccessible(String url) throws IOException {
        HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
        connection.setRequestMethod("HEAD"); // 使用HEAD请求，不下载实际内容，只获取响应头

        int responseCode = connection.getResponseCode();
        return (responseCode == HttpURLConnection.HTTP_OK);
    }
}
